/**
IndexDicml.java

(c) 2004-2005 by Thomas Krause
All rights reserved.

http://owl.gidoo.de
*/

package de.gidoo.owl;


import javax.swing.JFrame;
import java.util.*;
import java.io.*;
import java.awt.event.*;

/**
 * Display the indexing-dialog.
 * Indexing of a dictionary is done to decrease the loading time of the dicml-file.
 * @author Thomas Krause
 */
public class IndexDicml extends javax.swing.JDialog {
  
  public ResourceBundle i18n;
  
  /** Can be used by the the called worker-thread to tell that something went wrong*/
  public boolean errorOccured;
  
  private File dicmlFile;
  
  /** A seperate thread is used to perform the actual indexing. */
  private IndexDicmlWork workerThread;
  
  public ErrorDialog errorDlg;
  
  
  /**
   * Create a new instance of IndexDicml and start indexing.
   * @param pathToFile path to the dicml-file which shall be indexed
   * @param owner the frame which own this dialog
   * @param modal wether indexing should be done in the background or the user has to wait
   * @param i18n the resource-bundle which is used to show internationalized messages
   */
  public IndexDicml(String pathToFile, JFrame owner, boolean modal, ResourceBundle i18n)
  {
    super(owner, modal);
    
    errorOccured = false;
    
    this.i18n = i18n;
    errorDlg = new ErrorDialog(owner, true, i18n);
    
    initComponents();
    
    dicmlFile = new File(pathToFile);
    lInfo.setText(i18n.getString("indexFile1") + dicmlFile.getName() + i18n.getString("indexFile2"));
    
    //start new worker-thread
    workerThread = new IndexDicmlWork(dicmlFile, this);
    workerThread.start();
    
    setVisible(true);
  }
    
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  private void initComponents() {
    lInfo = new javax.swing.JLabel();
    jPanel1 = new javax.swing.JPanel();
    btOk = new javax.swing.JButton();
    btAbort = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    pbStatus = new javax.swing.JProgressBar();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle(i18n.getString("indexState"));
    setFont(new java.awt.Font("Dialog", 0, 10));
    lInfo.setFont(new java.awt.Font("Dialog", 0, 12));
    lInfo.setText("lInfo");
    lInfo.setPreferredSize(new java.awt.Dimension(24, 48));
    getContentPane().add(lInfo, java.awt.BorderLayout.NORTH);

    jPanel1.setLayout(new java.awt.BorderLayout());

    btOk.setFont(new java.awt.Font("Dialog", 0, 12));
    btOk.setText(i18n.getString("indexOk"));
    btOk.setEnabled(false);
    btOk.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btOkActionPerformed(evt);
      }
    });

    jPanel1.add(btOk, java.awt.BorderLayout.WEST);

    btAbort.setFont(new java.awt.Font("Dialog", 0, 12));
    btAbort.setText(i18n.getString("indexAbort"));
    btAbort.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btAbortActionPerformed(evt);
      }
    });

    jPanel1.add(btAbort, java.awt.BorderLayout.EAST);

    getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

    jPanel2.setLayout(new java.awt.BorderLayout());

    pbStatus.setFont(new java.awt.Font("Dialog", 0, 12));
    pbStatus.setPreferredSize(new java.awt.Dimension(250, 20));
    pbStatus.setStringPainted(true);
    jPanel2.add(pbStatus, java.awt.BorderLayout.NORTH);

    getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-373)/2, (screenSize.height-126)/2, 373, 126);
  }
  // </editor-fold>//GEN-END:initComponents

  private void btAbortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAbortActionPerformed
    workerThread.abort = true;
    errorOccured = true;
  }//GEN-LAST:event_btAbortActionPerformed

  private void btOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOkActionPerformed
    setVisible(false);
    dispose();
  }//GEN-LAST:event_btOkActionPerformed
  
   
  /**
   * Set the the status-bar and display informations depending on the actual state.
   * This is called by the worker-thread<br><br>
   * states: <br>
   * - read in the file (0-79%)<br>
   * - sort the entries (80%)<br>
   * - write the id-file (90%)<br>
   * - finished (101-200%)<br>
   * - an error occured (more than 200%)
   * @param alreadyDone how much work is done in percent
   */
  public void setStatusBarValue(int alreadyDone)
  {
    if(alreadyDone > 200)
    {
      // an error occured
      errorOccured = true;
      
      btAbort.setEnabled(false);
      btOk.setEnabled(true);
      lInfo.setText(i18n.getString("indexFinishedError"));
      pbStatus.setIndeterminate(false);
      pbStatus.setValue(100);
      pbStatus.setString("100%");
    }
    else if(alreadyDone > 100)
    {
      btAbort.setEnabled(false);
      btOk.setEnabled(true);
      lInfo.setText(i18n.getString("indexFinished"));
      pbStatus.setIndeterminate(false);
      pbStatus.setValue(100);
      pbStatus.setString("100%");
    }
    else if(alreadyDone == 80)
    {
      pbStatus.setIndeterminate(true);
      btAbort.setEnabled(false);
      pbStatus.setValue(80);
      pbStatus.setString("");
      lInfo.setText(i18n.getString("indexSorting"));
    }
    else if(alreadyDone == 90)
    {
      btAbort.setEnabled(false);
      pbStatus.setIndeterminate(true);
      pbStatus.setValue(80);
      pbStatus.setString("");
      lInfo.setText(i18n.getString("indexWriting"));
    }
    else
    {
      pbStatus.setValue(alreadyDone);
      pbStatus.setString(new String().valueOf(alreadyDone) + "%");
    }
    validate();
  }
  
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btAbort;
  private javax.swing.JButton btOk;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JLabel lInfo;
  private javax.swing.JProgressBar pbStatus;
  // End of variables declaration//GEN-END:variables
  
}
